package sixkey.aop;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import sixkey.utils.ResCodeEnum;
import sixkey.utils.ResResult;

/**
 * ClassName:PermissionAdvice
 * Package:sixkey.aop
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/6/22 - 14:11
 * @Version:v1.0
 */
@Slf4j
@Component
@Aspect
@Order(0) //数值越小优先级越高
public class PermissionAdvice {

    //切点
    @Pointcut("@annotation(sixkey.annotation.PermissionAnnotation)")
    public void pointcut(){};

    @Around("pointcut()")
    public Object permissionCheckFirst(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("==========参数校验============");
        //获取目标方法上的参数
        Object[] args = joinPoint.getArgs();
        String ids = args[0].toString();
        long id = Long.parseLong(ids);
        String username = args[1].toString();
        log.info("============id值===========" + id);
        log.info("============username值===========" + username);
        //校验逻辑
        if(id < 0){
            return ResResult.errorResult(ResCodeEnum.USERID_ERR);
        }
        if(StringUtils.isEmpty(username)){
            return ResResult.errorResult(ResCodeEnum.USERNAME_NOT_NULL);
        }
        if(!username.equals("admin")){
            return ResResult.errorResult(ResCodeEnum.NOT_ADMIN);
        }
        return joinPoint.proceed();
    }
}
